package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbItemFile;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbDemandListBo;
import org.dromara.system.domain.vo.WbDemandListVo;
import org.dromara.system.domain.WbDemandList;
import org.dromara.system.mapper.WbDemandListMapper;
import org.dromara.system.service.IWbDemandListService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 需求一览Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@RequiredArgsConstructor
@Service
public class WbDemandListServiceImpl implements IWbDemandListService {

    private final WbDemandListMapper baseMapper;

    @Override
    public List<WbDemandListVo> queryGroupById(String ItemID) {

        return baseMapper.selectDemandListByWbId(ItemID);
    }

    /**
     * 查询需求一览
     */
    @Override
    public WbDemandListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询需求一览列表
     */
    @Override
    public TableDataInfo<WbDemandListVo> queryPageList(WbDemandListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbDemandList> lqw = buildQueryWrapper(bo);
        Page<WbDemandListVo> result = baseMapper.selectVoPage(pageQuery.build(),lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询需求一览列表
     */
    @Override
    public List<WbDemandListVo> queryList(WbDemandListBo bo) {
        LambdaQueryWrapper<WbDemandList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbDemandList> buildQueryWrapper(WbDemandListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbDemandList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardId()), WbDemandList::getSubStandardId, bo.getSubStandardId());
        lqw.like(StringUtils.isNotBlank(bo.getSubStandardName()), WbDemandList::getSubStandardName, bo.getSubStandardName());
        lqw.eq(StringUtils.isNotBlank(bo.getBagId()), WbDemandList::getBagId, bo.getBagId());
        lqw.like(StringUtils.isNotBlank(bo.getBagName()), WbDemandList::getBagName, bo.getBagName());
        lqw.eq(bo.getHighestPrice() != null, WbDemandList::getHighestPrice, bo.getHighestPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), WbDemandList::getItemId, bo.getItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getProvincePurchaseRequisitionNumber()), WbDemandList::getProvincePurchaseRequisitionNumber, bo.getProvincePurchaseRequisitionNumber());
        lqw.orderByAsc(WbDemandList::getSubStandardId,WbDemandList::getBagId,WbDemandList::getId);
        return lqw;
    }

    /**
     * 新增需求一览
     */
    @Override
    public Boolean insertByBo(WbDemandListBo bo) {
        WbDemandList add = MapstructUtils.convert(bo, WbDemandList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改需求一览
     */
    @Override
    public Boolean updateByBo(WbDemandListBo bo) {
        WbDemandList update = MapstructUtils.convert(bo, WbDemandList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbDemandList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除需求一览
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<WbDemandListVo> queryList1(WbDemandListBo bo) {
        LambdaQueryWrapper<WbDemandList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getItemId()), WbDemandList::getItemId, bo.getItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardId()), WbDemandList::getSubStandardId, bo.getSubStandardId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubStandardName()), WbDemandList::getSubStandardName, bo.getSubStandardName());
        lqw.eq(StringUtils.isNotBlank(bo.getBagId()), WbDemandList::getBagId, bo.getBagId());
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public TableDataInfo<WbDemandListVo> queryCountPageList(Long id,String subStandardName, PageQuery pageQuery) {
        Page<WbDemandListVo> wbDemandListVoPage = baseMapper.selectCountVoPage(id,subStandardName,pageQuery.build());
        return TableDataInfo.build(wbDemandListVoPage);
    }

}
